package com.gobuy.search.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.gobuy.common.pojo.Page;
import com.gobuy.search.pojo.SkuInfo;
import com.gobuy.search.service.ESSearchSerice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;
import java.util.Set;

/**
 * @author Gaozexi on 2020/6/10 0:17
 * Step by Step  and Stand on,  You Are The Best Investment!
 */
@Controller
@RequestMapping("/search")
public class ESSearchController {

    @Autowired
    private ESSearchSerice searchService;

    @Autowired
    private ObjectMapper objectMapper;

    @GetMapping("/list")
    public String list(@RequestParam Map<String, String> searchMap, Model model) {
        //处理特殊符号
        this.handleSearchMap(searchMap);
        //获取查询结果
        Map resultMap = searchService.search(searchMap);
        Object brandList = resultMap.get("brandList");
        System.out.println("brandList = " + brandList);

        //封装分页对象
        Long total = Long.parseLong(resultMap.get("total") + "");
        Integer current = Integer.parseInt(resultMap.get("pageNum") + "");
        Page<SkuInfo> page = new Page<>(total, current, Page.PAGE_SIZE);

        model.addAttribute("result", resultMap)
                .addAttribute("searchMap", searchMap)
                .addAttribute("page", page);


        //拼装 查询条件,将用户点击的查询条件附带在url传入,拼装条件即拼装url
        StringBuilder url = new StringBuilder("/search/list");
        if (searchMap != null && searchMap.size() > 0) {
            url.append("?");
            for (String searchKey : searchMap.keySet()) {
                //排除默认的关键字
                if (!"sortRule".equals(searchKey) && !"sortField".equals(searchKey) && !"pageNum".equals(searchKey)) {
                    url.append(searchKey).append("=").append(searchMap.get(searchKey)).append("&");
                }
                //排除最后一个多余的&
                String urlStr = url.toString();
                urlStr = urlStr.substring(0, urlStr.length() - 1);
                //存贮在model中
                model.addAttribute("url", urlStr);
            }
        } else {//没有查询条件
            model.addAttribute("url", url);
        }

        return "search";
    }


    @GetMapping
    @ResponseBody
    public Map search(@RequestParam Map<String, String> searchMap) {
        //处理特殊符号 TODO 不理解
        this.handleSearchMap(searchMap);
        Map searchResult = searchService.search(searchMap);
        return searchResult;
    }

    private void handleSearchMap(Map<String, String> searchMap) {
        Set<String> searchKeys = searchMap.keySet();
        for (String searchKey : searchKeys) {
            if (searchKey.startsWith("spec_")) {
                searchMap.put(searchKey, searchMap.get(searchKey).replace("+", "%2B"));
            }
        }

    }
}
